package com.gz.furnitures.dao.impl;

import com.gz.furnitures.dao.BasicDao;
import com.gz.furnitures.dao.OrderItemDao;
import com.gz.furnitures.entity.OrderItem;

import java.util.Collections;
import java.util.List;

public class OrderItemDaoImpl extends BasicDao<OrderItem> implements OrderItemDao {
    @Override
    public int saveOrderItem(OrderItem orderItem) {
        String sql = "INSERT into `order_item`(`id`,`name`,price,`count`," +
                "total_price,`order_id`) VALUES(?,?,?,?,?,?)";
        return update(sql,orderItem.getId(),orderItem.getName(),orderItem.getPrice(),orderItem.getCount(),orderItem.getTotalPrice(),orderItem.getOrderId());
    }

    @Override
    public int getTotalRowByOrderId(String orderId) {
        String sql="select count(*) from  `order_item` where order_id =?";
        //注意，这里不能写成return (Integer)queryScalar(sql);
        // 他返回的不是Integer类型，会报cast异常
        return ((Number)queryScalar(sql,orderId)).intValue();
    }

    @Override
    public List<OrderItem> getOrderItemsByOrderId(String orderId) {
        String sql="select `id`,`name`,price,`count`,total_price totalPrice," +
                "`order_id` orderId from `order_item` where order_id =?";
        return queryMulti(sql,OrderItem.class,orderId);
    }


    @Override
    public List<OrderItem> getPageOrderItemsByOrderId(int begin, int pageSize,String orderId) {
        //注意这里不能用*，因为数据库里面的列名和类里面的元素名不一样，否则反射找不到
        String sql="select `id`,`name`,price,`count`,total_price totalPrice,`order_id` orderId from `order_item` where order_id =? limit ?,?";
        return queryMulti(sql,OrderItem.class,orderId,begin,pageSize);
    }

}
